import { ElMessage } from "element-plus";
import { getProductModelDetail, updateProductModelUpdate } from "@/api/modules/carType";
import { useDict } from "@/hooks/useDict";
import { useUtile } from "./useUtil";
const dictInit = async (selectList: any, dataList: any) => {
  const { getDicList } = useDict();
  const { changeOptionsData, fittingsFn } = useUtile();
  try {
    let categoryIdList = dataList.map(item => item.categoryId);
    categoryIdList = [...new Set(categoryIdList)];
    let parentIdList = dataList.map(item => item.categoryId).concat(dataList.map(item => item.typeId));
    parentIdList = [...new Set(parentIdList)].join(",");
    const data = await getDicList({ parentIdList });
    selectList.value = changeOptionsData(data, categoryIdList);
    // 配件特殊处理
    for (let i = 0; i < dataList.length; i++) {
      if (dataList[i].type === 4) {
        const da = await fittingsFn(dataList[i].value);
        const obj = {};
        obj[dataList[i].value] = da;
        selectList.value[dataList[i].value] = changeOptionsData(da)?.[dataList[i].value];
      }
    }
  } catch (error) {
    console.log(error);
  }
};

// const MOCK =
//   '{"id":"777777", "remark":"测嘶","typeId":"1788369471023349760","code":"","name":"33","picture":"/product/jpg/9791d65e8f534120ac0fb4489f2a4e66.jpg","specifiedLoad":234,"specifiedLoadDistance":456,"loadHeight":667,"loadLineChartPath":"/product/png/d92a68be05b347e6b1f3a00f1f7b9854.png","radarHeight":767,"forkLength":678,"forkWidth":8778,"forkThickness":7878,"groundHeightLow":788,"groundHeightHigh":78,"channelWidth":788,"channelWidthSafe":788,"turningRadius":788,"turningRadiusMin":788,"wheelDrivingSize":788,"wheelLoadSize":788,"wheelBalanceSize":788,"groundClearanceMin":788,"climbFull":78,"climbEmpty":78,"length":78,"width":7,"height":87,"weight":788,"driveMotorPower":788,"speedDriveFull":78,"speedDriveEmpty":78,"liftMotorPower":78,"liftSpeedFull":767,"liftSpeedEmpty":878,"dropSpeedFull":55,"dropSpeedEmpty":785,"chargeRate":"","title":"","keywords":"","description":"","propertyList":[{"typeId":"1844684204030296064","typeName":"充电模式","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1844684853618933760","sourceName":"自动"}],"contentOptional":[{"sourceId":"1844684853618933760","sourceName":"自动"}]},{"typeId":"1848181355645501440","typeName":"充电方式","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1848256020518797312","sourceName":"正充"}],"contentOptional":[{"sourceId":"1848256020518797312","sourceName":"正充"}]},{"typeId":"1848181466345766912","typeName":"换电方式","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1848256870012157952","sourceName":"拆装"}],"contentOptional":[{"sourceId":"1848256870012157952","sourceName":"拆装"}]},{"typeId":"1848181503024955392","typeName":"电池类型","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1848257061847040000","sourceName":"磷酸铁锂"}],"contentOptional":[{"sourceId":"1848257061847040000","sourceName":"磷酸铁锂"}]},{"typeId":"1848181546922541056","typeName":"容量（Ah）","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1854035370367254528","sourceName":"48V315Ah"},{"sourceId":"1854035575045095424","sourceName":"48V30Ah"}],"contentOptional":[]},{"typeId":"1848181596771844096","typeName":"续航时间（h）","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1854033085696311296","sourceName":"5~6h"}],"contentOptional":[]},{"typeId":"1848181706494836736","typeName":"电池品牌-电芯品牌","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1848257742557413376","sourceName":"泰坦-国轩"},{"sourceId":"1848257797448269824","sourceName":"聚云-国轩"}],"contentOptional":[]},{"typeId":"1848181749184462848","typeName":"充电桩型号","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"value":"1","content":[{"sourceId":"1848258406398296064","sourceName":"施能：CZC7-20A/54.8V） 48V10A"}],"contentOptional":[]},{"typeId":"1848181829790597120","typeName":"控制器","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1","content":[{"sourceId":"1848265913854328832","sourceName":"AiTEN"}],"contentOptional":[{"sourceId":"1848265913854328832","sourceName":"AiTEN"}]},{"typeId":"1848181865685450752","typeName":"2D-SLAM激光导航","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848181904877027328","typeName":"3D-SLAM激光导航","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848181940167901184","typeName":"无线Wi-Fi通讯","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848181975404249088","typeName":"5G通讯","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182010753843200","typeName":"手持操控器","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1","content":[{"sourceId":"1848266786227617792","sourceName":"手柄"}],"contentOptional":[{"sourceId":"1848266786227617792","sourceName":"手柄"}]},{"typeId":"1848182046581587968","typeName":"人机操作界面 ","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182092739903488","typeName":"前向底部避障配置（点云避障）","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1","content":[{"sourceId":"1848267014020268032","sourceName":"富锐-C200"}],"contentOptional":[{"sourceId":"1848267014020268032","sourceName":"富锐-C200"}]},{"typeId":"1848182158850523136","typeName":"前向立体避障配置","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182195928170496","typeName":"斜射雷达","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182227133792256","typeName":"后退底部避障配置（叉根）","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182261040545792","typeName":"牙尖平面避障（雷达）","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1","content":[{"sourceId":"1848268360635121664","sourceName":"兴颂DE4211"}],"contentOptional":[{"sourceId":"1848268360635121664","sourceName":"兴颂DE4211"}]},{"typeId":"1848182305181401088","typeName":"牙尖光电防撞","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182333400678400","typeName":"防撞条","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182363540946944","typeName":"急停按钮分布","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182388497055744","typeName":"语音播报器","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182421082603520","typeName":"行车指示灯（7色，状态显示/左右转弯）","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182452476968960","typeName":"行车示廓灯（蓝光灯）","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182482227167232","typeName":"三色警示灯","categoryId":"1844678042417364992","categoryName":"configList","type":null,"value":"1"},{"typeId":"1848182509318176768","typeName":"行车记录仪","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182542562230272","typeName":"条码识别","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182604784730112","typeName":"RFID识别 ","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182632144175104","typeName":"栈板识别","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"typeId":"1848182668110331904","typeName":"外形检测 ","categoryId":"1844678042417364992","categoryName":"configList","type":3,"value":"1"},{"uuid":"8735847b-2782-4053-ba85-95f1e00f8d14","typeId":"1854430380229132288","typeName":"测试电池","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"content":[{"sourceId":"1854430609569480704","sourceName":"测试1"}],"contentOptional":[{"sourceId":"1854430609569480704","sourceName":"测试1"}]},{"uuid":"ab3e7312-3ea1-43fa-960b-0d84f585a035","typeId":"1854430380229132288","typeName":"测试电池","categoryId":"1844677957868584960","categoryName":"batteryInfo","type":3,"content":[{"sourceId":"1854430609569480704","sourceName":"测试1"}],"contentOptional":[{"sourceId":"1854430609569480704","sourceName":"测试1"}]}]}';
export const useEdit = (options: any) => {
  const { formData, title, emits, drawerVisible, ruleFormRef, id, selectList } = options;
  const { transformDataReq, transformDataRes } = useUtile();
  // 初始化
  const initData = async () => {
    const { data } = await getProductModelDetail({ id: id.value }); //  JSON.parse(MOCK) ||
    // 字典数据
    dictInit(selectList, data.propertyList);

    data.propertyList = transformDataRes(data.propertyList);
    formData.value = data;
  };
  // 提交事件
  const handleSubmit = () => {
    ruleFormRef.value!.validate(async (valid: boolean) => {
      if (!valid) return;
      try {
        await updateProductModelUpdate(transformDataReq(formData.value));
        ElMessage.success({ message: `${title.value}车型成功！` });
        emits("refresh");
        drawerVisible.value = false;
      } catch (error) {
        console.log(error);
      }
    });
  };

  return {
    handleSubmit,
    initData
  };
};
